package be.ac.ua.pats.thesisrobotica.robot.RR.sensors;

//~--- non-JDK imports --------------------------------------------------------

import be.ac.ua.pats.thesisrobotica.robot.RR.Refractive;
import be.ac.ua.pats.thesisrobotica.robot.communication.Connection;
import be.ac.ua.pats.thesisrobotica.robot.constants.RobotConstants;
import be.ac.ua.pats.thesisrobotica.robot.exceptions.InitializationException;
import lejos.nxt.MotorPort;


/**
 * @author Jimmy
 */
public class Actuator extends Sensor implements Refractive {
    lejos.nxt.Motor sensor;

    public Actuator(MotorPort port) {
        this(null,port);
    }

    public Actuator(Connection connection,MotorPort port) {
               super(connection);

        sensor      = new lejos.nxt.Motor(port);
        type        = RobotConstants.Types.MOTOR;
        initialized = true;
    }

    @Override
    public void setValue(String value) {
        switch (Integer.valueOf(value)) {
        case 0 :
            sensor.stop();

            break;

        case -1 :
            sensor.backward();

            break;

        case 1 :
            sensor.forward();

            break;
        }
    }

    /**
     * @return The current sensor reading
     */
    @Override
    public int getRealValue() throws InitializationException {
        if (initialized) {
            return sensor.isForward()
                   ? 1
                   : (sensor.isBackward()
                      ? -1
                      : 0);
        } else {
            throw new InitializationException(this.id);
        }
    }




    @Override
    public String getValue() throws InitializationException {
        if (initialized) {
            return sensor.isForward()
                   ? "1"
                   : (sensor.isBackward()
                      ? "-1"
                      : "0");
        } else {
            throw new InitializationException(this.id);
        }
    }

}
